(function() {
    'use strict';

angular
    .module('lighter')
    .controller('MainController', MainController);

    /** @ngInject */
    function MainController(moment, nodeAPIservice) {
        //vm is short for view model
        var vm = this;

        vm.page = {
            size: 10,
            index: 1  //由pagination指令中的ng-model动态改变
        };
        
        //数据排序
        vm.sort = {
            column: 'id',
            direction: 1,
            toggle: function(column) {
                if (column.sortable === false) return;

                if (vm.sort.column === column.name) {
                    vm.sort.direction = -vm.sort.direction || 1;
                } else {
                    vm.sort.column = column.name;
                    vm.sort.direction = 1;
                }
            }
        };

        // 构建表头模拟数据
        vm.columns = [
            {
                label: 'ID',
                name: 'id',
                type: 'string'
            },
            {
                label: '标题',
                name: 'title',
                type: 'string'
            },
            {
                label: '访问数',
                name: 'visit_count',
                type: 'number'
            },
            {
                label: '回复数',
                name: 'reply_count',
                type: 'number'
            },
            {
                label: '创建日期',
                name: 'create_at',
                type: 'string'
            }
        ];

        nodeAPIservice.getTopics().success(function(response) {
            // 供页面中使用的函数
            vm.items = response.data;
        }) 
    }

})();
